# 使用 CentOS 7 作为基础镜像
FROM centos:centos7

LABEL description="The King of Salted Fish" version="1.0" owner="Tim" create_by="Tim"

# 设置非交互式安装，避免安装过程中的任何交互提示
ENV DEBIAN_FRONTEND=noninteractive 
# 配置环境变量 
ENV LC_ALL=en_US.UTF-8 
ENV PUBLIC_IP=127.0.0.1
ENV DB_USER_PWD=123456
 
# 安装环境
WORKDIR /home/proj

# 备份原始 CentOS repo 配置并替换为阿里云的镜像源
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak && \
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 安装所需的依赖
RUN yum -y groupinstall "Development Tools" && \
    yum -y install epel-release && \
    yum -y install \
    wget \
    curl \
    unzip \
    vim \
    gcc \
    make \
    libxml2-devel \
    libjpeg-devel \
    libpng-devel \
    libmcrypt-devel \
    libicu-devel \
    libxslt-devel \
    libzip-devel \
    zlib-devel \ 
    openssl-devel \
    autoconf \
    bzip2-devel \
    httpd \
    yum-utils \
    dmidecode \
    && yum clean all

# 安装 MySQL 5.7
RUN rpm  -Uvh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm && \
    rpm  --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 && \
    yum -y install mysql-server   && \
    yum clean all
 
# 安装 PHP 7.2
RUN yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm && \
    yum -y install yum-utils && \
    yum-config-manager --enable remi-php72 && \
    yum -y install php php-cli php-fpm php-mysql php-common php-json php-opcache php-mbstring php-xml php-gd php-zip && \
    yum clean all

# 安装 MongoDB 4
RUN echo "[mongodb-org-4.4]" > /etc/yum.repos.d/mongodb-org-4.4.repo && \
    echo "name=MongoDB Repository" >> /etc/yum.repos.d/mongodb-org-4.4.repo && \
    echo "baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el7-4.4/" >> /etc/yum.repos.d/mongodb-org-4.4.repo && \
    echo "gpgcheck=1" >> /etc/yum.repos.d/mongodb-org-4.4.repo && \
    echo "enabled=1" >> /etc/yum.repos.d/mongodb-org-4.4.repo && \
    echo "gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc" >> /etc/yum.repos.d/mongodb-org-4.4.repo && \
    rpm --import https://www.mongodb.org/static/pgp/server-4.4.asc && \
    yum -y install mongodb-org && \
    yum clean all
  
# 安装 Redis 7.2.4
RUN yum -y install https://rpms.remirepo.net/enterprise/7/remi/x86_64/redis-7.2.4-1.el7.remi.x86_64.rpm && \
    yum clean all
 
# 安装openresty
RUN wget https://openresty.org/download/openresty-1.19.3.1.tar.gz && \
    tar -zxvf openresty-1.19.3.1.tar.gz && \
    mv openresty-1.19.3.1 openresty && \
    cd openresty && \ 
    ./configure && \
    make && \
    make install

#安装nginx
RUN wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.18.0-1.el7.ngx.x86_64.rpm && \
    rpm -ivh nginx-1.18.0-1.el7.ngx.x86_64.rpm && \
    yum clean all

# 复制相关文件
COPY home.tar.gz / 
# 配置 Nginx 
COPY game.conf /etc/nginx/conf.d/game.conf
# 修改Redis配置
COPY redis.conf /etc/redis/redis.conf

# 解压
RUN tar -xzf /home.tar.gz -C / && chmod 777 -R /home/proj/ && \
    cp /home/proj/openresty/build/nginx-1.19.3/conf/fastcgi_params /home/proj/apiserver/conf/fastcgi_params && \
    rm /home.tar.gz

# 需要的目录
RUN mkdir -p /data/db && \
    chown 777 -R  /data/db && \
    mkdir -p /run/php-fpm && \
    chown 777 -R /run/php-fpm 

# 修改 MongoDB 配置文件，禁用授权
# RUN sed -i 's/authorization: disabled/authorization: disabled off/' /etc/mongod.conf || echo 'security:\n  authorization: disabled' >> /etc/mongod.conf 
RUN echo -e '\nsecurity:\n  authorization: disabled off' >> /etc/mongod.conf
 
#创建符号链接  libssl
RUN ln -s /home/proj/skynet/luaclib/libssl.so.1.0.0 /usr/lib64/libssl.so.1.0.0 && \
    ln -s /home/proj/skynet/luaclib/libcrypto.so.1.0.0 /usr/lib64/libcrypto.so.1.0.0
 
# 复制启动脚本
COPY startup.sh /startup.sh
RUN chmod +x /startup.sh
 
# 设置默认的服务启动命令
CMD ["bash", "-c", "/startup.sh"]
 
